<?php

class Adept_Db_MySql_Statement 
{
    
    protected $connection;
    protected $statement;
    protected $parameters = array();

    public function __construct($connection, $sql) 
    {
        $this->statement = $sql;
        $this->connection = $connection;
    }

    public function setBoolean($name, $value) 
    {
         $this->parameters[$name] = is_null($value) ? 'NULL' : (($value) ? '1' : '0');
    }
    
    public function setString($name, $value) 
    {
         $this->parameters[$name] = is_null($value) ? 
         'NULL' : "'" . mysql_real_escape_string((string) $value) . "'";
    }
    
    protected function _setDate($name, $value, $format) 
    {
        if (is_int($value))
            $this->parameters[$name] = "'" . date($format, $value) ."'";
        else if (is_string($value)) {
            $this->parameters[$name] = "'" . mysql_real_escape_string($value) . "'";
        } else {
            $this->parameters[$name] = "NULL";
        }
    }
    
    public function setDate($name, $value) 
    {
        $this->_setDate($name, $value, 'Y-m-d');
    }
    
    public function setTime($name, $value) 
    {
        $this->_setDate($name, $value, 'H:i:s');
    }
    
    public function setTimeStamp($name, $value) 
    {
        $this->_setDate($name, $value,'Y-m-d H:i:s');
    }
    
    public function setDateTime($name, $value) 
    {
        $this->_setDate($name, $value,'Y-m-d H:i:s');
    }
    
    public function setInteger($name, $value) 
    {
        $this->parameters[$name] = is_null($value) ? 'NULL' : intval($value);
    }
 
    public function setFloat($name, $value) 
    {
        $this->parameters[$name] = is_null($value) ? 'NULL' : floatval($value);
    }
    
    public function setNull($name) 
    {
        $this->parameters[$name] = 'NULL';
    }
    
    public function setText($name, $value) 
    {
         $this->parameters[$name] = is_null($value) ? 
         'NULL' : "'" . mysql_real_escape_string((string) $value) . "'";        
    }
    
    public function setVarChar($name, $value) 
    { 
         $this->parameters[$name] = is_null($value) ? 
         'NULL' : "'" . mysql_real_escape_string((string) $value) . "'";
    
    }    
 
    public function set($name, $value)
    {
        if (is_string($value))
            $this->setString($name, $value);
        else if (is_int($value))
            $this->setInteger($name, $value);
        else if (is_bool($value))
            $this->setBoolean($name, $value);
        else if (is_float($value))
            $this->setFloat($name, $value);
        else
            $this->setNull($name);
    }
    
    public function setAs($name, $value, $type = '') 
    {
        $setters = array(
            Adept_Db_ColumnInfo::BOOLEAN => 'setBoolean',
            Adept_Db_ColumnInfo::STRING => 'setString',
            Adept_Db_ColumnInfo::TEXT => 'setText',
            Adept_Db_ColumnInfo::INTEGER => 'setInteger',
            Adept_Db_ColumnInfo::FLOAT => 'setFloat',
            Adept_Db_ColumnInfo::DATE => 'setDate',
            Adept_Db_ColumnInfo::TIME => 'setTime',
            Adept_Db_ColumnInfo::TIMESTAMP => 'setTimeStamp',
            Adept_Db_ColumnInfo::DATETIME => 'setDateTime'            
        );
        
        if (isset($setters[$type])) {
            $setter = $setters[$type];
            $this->$setter($name, $value);
        } else {
            $this->set($name, $value);
        }
    }
    
    public function setAsIs($name, $value) 
    {
        $this->parameters[$name] = $value;
    }
    
    public function getSql() 
    {
        $sql = $this->statement;
        foreach ($this->parameters as $key => $value) {
            $sql = str_replace(':' . $key . ':', $value, $sql);
        }
        return $sql;
    }
    
    public function execute() 
    {
        return (boolean) $this->connection->execute($this->getSql());
    }
}